package Utility;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;

public class webconnect {
	
	Properties OR= null;
	Properties CONFIG= null;
	public WebDriver driver= null;
	Logger APPLICATION_LOGS = Logger.getLogger("test");
	public webconnect(){
		
		if(OR==null)
		{
			
			try{
			//initialize OR
			
			OR= new Properties();
			FileInputStream fs= new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\java\\config\\OR.properties");
			OR.load(fs);
			
			//initialize CONFIG to corresponding environment
			CONFIG= new Properties();
			fs = new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\java\\config\\"+OR.getProperty("testEnv")+"_config.properties");
			CONFIG.load(fs);
			
			//System.out.println(OR.getProperty("LoginUserName"));
			//System.out.println(CONFIG.getProperty("LoginUrl"));
			
			
				
			}catch(Exception e){
				System.out.println("Error while initializing");
			}
		}
		
	}
	
	public void OpenBrowser(String browsertype)
	{
		
		if(browsertype.equals("Firefox"))
		{
			driver = new FirefoxDriver();
		}
		

		else if(browsertype.equals("Chrome"))
		{
			System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//Chrome//chromedriver.exe");
			driver = new ChromeDriver();
		}
		else if(browsertype.equals("IE"))
		{
			
			System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"//IE//IEDriverServer.exe");
			driver = new InternetExplorerDriver();
		}
		
		//maximize browser
		driver.manage().window().maximize();
		//implicit wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		

	}

	//Navigates to a URL
	public void Navigate(String URL)
	{
		driver.get(CONFIG.getProperty(URL));
		driver.findElement(By.xpath((OR.getProperty("LoginLinkButton")))).click();
	
	}

	//Clicking on any object
	public void click(String ObjectName)
	{
		
		try {
			this.highlightElement(ObjectName);
			driver.findElement(By.xpath(OR.getProperty(ObjectName))).click();
			Thread.sleep(2L);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void type(String text, String ObjectName)
	{
		
		try {
			this.highlightElement(ObjectName);
			driver.findElement(By.xpath(OR.getProperty(ObjectName))).sendKeys(text);
			Thread.sleep(2L);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public String getText(String ObjectName)
	{
		
		try {
			Thread.sleep(2L);
			this.highlightElement(ObjectName);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String text=  driver.findElement(By.xpath(OR.getProperty(ObjectName))).getText();
		return text;
		
	}

	public void select(String text, String ObjectName)
	{
		this.highlightElement(ObjectName);
		driver.findElement(By.xpath(OR.getProperty(ObjectName))).sendKeys(text);
		
	}


	public boolean isElementPresent(String ObjectName){
		
		
		
		Boolean present = null;	
		
	try 
	{
		
		present  = driver.findElement(By.xpath(OR.getProperty(ObjectName))).isDisplayed();
		
		if(present)
		{
			
			this.highlightElement(ObjectName);
			Thread.sleep(2L);
		return true;
		}
		else
		return false;

	} 

	catch(Throwable isElementPresentException) 
	{

		// Log error
		APPLICATION_LOGS.debug("Element is not present");

		return false;

	}
		
	}


	//Clicking on any object
		public String GetElementText(String ObjectName)
		{
			this.highlightElement(ObjectName);
			String ElementText = driver.findElement(By.xpath(OR.getProperty(ObjectName))).getText();
			return ElementText;
			
		}

		// highlight the element on which action will be performed
	    public void  highlightElement(String objectName) {

		try {
			
		    for (int i = 0; i < 3; i++) {
			JavascriptExecutor js = (JavascriptExecutor) driver;
			js.executeScript(
				"arguments[0].setAttribute('style', arguments[1]);",driver.findElement(By.xpath(OR.getProperty(objectName))),"color: red; border: 2px solid red;");
			Thread.sleep(2L);
		    }

		}

		catch (Throwable t) {
		    APPLICATION_LOGS.debug("Error came : " + t.getMessage());
		}

	    }
	
	    
	    
	    public void  mouseOverWebElement(String objectName) 
	  	{
	  		try{
	  			
	  		Actions builder = new Actions(driver);		
	  		builder.moveToElement(driver.findElement(By.xpath("//a[text()='Hi Testme !']"))).build().perform();	
	  		
	  		APPLICATION_LOGS.debug( objectName +" Mouse hovered");
	  		Thread.sleep(5l);
	  		this.highlightElement(objectName);
	  		Thread.sleep(2l);
	  		}catch (Exception e){
	  			System.out.println("Fail"+e.getMessage());
	  		}
	  	}
	
//quitting the browser
public void quitBrowser()
{
	
	driver.quit();
}
	
	
	

}
